//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "Histogram.h"
using namespace Cephei::QL::Math::Statistics;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Math::Statistics::CHistogram::CHistogram () 
{
	_pSpinlock = new boost::detail::spinlock ();
    try
    {
#ifdef HANDLE
        _phHistogram = NULL;
#endif
        _ppHistogram = new boost::shared_ptr<QuantLib::Histogram> (new QuantLib::Histogram ( ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Math::Statistics::CHistogram::CHistogram (boost::shared_ptr<QuantLib::Histogram>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phHistogram = NULL;
#endif
	_ppHistogram = &childNative;
    
}
Cephei::QL::Math::Statistics::CHistogram::CHistogram (QuantLib::Histogram& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phHistogram = NULL;
#endif
	_ppHistogram = new boost::shared_ptr<QuantLib::Histogram> (&childNative);
    
    _HistogramOwner = owner;
    
}

Cephei::QL::Math::Statistics::CHistogram::CHistogram (CHistogram^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phHistogram = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppHistogram = new boost::shared_ptr<QuantLib::Histogram> (copy->GetShared());
        
    }
}
Cephei::QL::Math::Statistics::CHistogram::CHistogram (System::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phHistogram = NULL;
#endif
	if (!t->IsSubclassOf(CHistogram::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Math::Statistics::CHistogram::CHistogram (QuantLib::Handle<QuantLib::Histogram>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phHistogram = &childNative;
	_ppHistogram = &static_cast<boost::shared_ptr<QuantLib::Histogram>>(childNative.currentLink());
    
    _HistogramOwner = owner;
}
Cephei::QL::Math::Statistics::CHistogram::CHistogram (QuantLib::Handle<QuantLib::Histogram> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phHistogram = &childNative;
	_ppHistogram = &static_cast<boost::shared_ptr<QuantLib::Histogram>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Math::Statistics::CHistogram::CHistogram (QuantLib::Histogram childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phHistogram = NULL;
#endif
	_ppHistogram = new boost::shared_ptr<QuantLib::Histogram> (new QuantLib::Histogram (childNative));
    
}
#endif

Cephei::QL::Math::Statistics::CHistogram::~CHistogram ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppHistogram != NULL)
    {
	    delete _ppHistogram;
        _ppHistogram = NULL;
    }
}
Cephei::QL::Math::Statistics::CHistogram::!CHistogram ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppHistogram != NULL)
    {
	    delete _ppHistogram;
    }
}
QuantLib::Histogram& Cephei::QL::Math::Statistics::CHistogram::GetReference ()
{
    if (_ppHistogram == NULL) throw gcnew NativeNullException ();
	return **_ppHistogram;
}
boost::shared_ptr<QuantLib::Histogram>& Cephei::QL::Math::Statistics::CHistogram::GetShared ()
{
    if (_ppHistogram == NULL) throw gcnew NativeNullException ();
	return *_ppHistogram;
}
QuantLib::Histogram* Cephei::QL::Math::Statistics::CHistogram::GetPointer ()
{
    if (_ppHistogram == NULL) throw gcnew NativeNullException ();
	return &**_ppHistogram;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::Histogram>& Cephei::QL::Math::Statistics::CHistogram::GetHandle ()
{
	if (_phHistogram == NULL)
	{
		_phHistogram = new Handle<QuantLib::Histogram> (*_ppHistogram);
	}
	return *_phHistogram;
}
#endif
bool Cephei::QL::Math::Statistics::CHistogram::HasNative () 
{
	return (_ppHistogram != NULL);
}

QL::Math::Statistics::Histogram::AlgorithmEnum Cephei::QL::Math::Statistics::CHistogram::Algorithm::get ()
{
    try
    {
    	QuantLib::Histogram::Algorithm _rv = (QuantLib::Histogram::Algorithm)(*_ppHistogram)->algorithm ( );   
        QL::Math::Statistics::Histogram::AlgorithmEnum _nrv = (QL::Math::Statistics::Histogram::AlgorithmEnum)_rv;
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
UInt64 Cephei::QL::Math::Statistics::CHistogram::Bins::get ()
{
    try
    {
    	QuantLib::Size _rv = (QuantLib::Size)(*_ppHistogram)->bins ( );   
        UInt64 _nrv = (UInt64)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::IVector<Double>^ Cephei::QL::Math::Statistics::CHistogram::Breaks::get ()
{
    try
    {
    	std::vector<QuantLib::Real>& _rv = (std::vector<QuantLib::Real>&)(*_ppHistogram)->breaks ( );   
        Cephei::IVector<Double>^ _nrv = gcnew CoVector<Double> (gcnew CDoubleVector (_rv));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
UInt64 Cephei::QL::Math::Statistics::CHistogram::Counts (UInt64 i)
{
    try
    {
        QuantLib::Size _i = (QuantLib::Size)ValueHelper::Convert (i);
    	QuantLib::Size _rv = (QuantLib::Size)(*_ppHistogram)->counts ( _i );   
        UInt64 _nrv = (UInt64)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Boolean Cephei::QL::Math::Statistics::CHistogram::Empty::get ()
{
    try
    {
    	bool _rv = (bool)(*_ppHistogram)->empty ( );   
        Boolean _nrv = (Boolean)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Math::Statistics::CHistogram::Frequency (UInt64 i)
{
    try
    {
        QuantLib::Size _i = (QuantLib::Size)ValueHelper::Convert (i);
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppHistogram)->frequency ( _i );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Math::Statistics::IHistogram^ Cephei::QL::Math::Statistics::CHistogram_Factory::Create ()
{
    return gcnew CHistogram ();
}
